*     *****************************************************************   *;
*     *****************************************************************   *; 
*   Date:          December 27, 2021                               	      *;
*   Authors:       Lisa Dellmuth and Jonas Tallberg				          *;  
*   Purpose: 	   Analyses for book Legitimacy Politics: Elite  		  *;
*    Communication and Public Opinion in Global Governance (CUP, 2023)    *;
*     ****************************************************************    *;
*     ****************************************************************    *;

*Figures from Afribarometer

*Years for SADC available for 2002, 2003, 2014 and 2015

use dataset_Latinobarometer_Afrobarometer_2000-2015-wide.dta, clear
keep if au_trust!=.
by year, sort:tab country
sort country year
drop if country_name==""
tab country 
tab au_trust year if country==426
*72 Botswana
*132 Cape Verde
*288 Ghana
*404 Kenya<<<
*426 Lesotho
*454 Malawi<<<
*466 Mali<<<
*508 Mozambique
*566 Nigeria<<<
*686 Senegal
*710 ZAF<<<
*800 lesnda<<<
*834 Tanzania
*894 Zambia
*716 Zimbabwe

/*
use "/Users/mariadellmuth/Dropbox/Papers/Sociallegitimacy/Legitimacy Book/Analysis/patterns/Afro- and Latinobarometer/dataset_Latinobarometer_Afrobarometer_2000-2015-wide.dta", clear
sum withinwt
ed country year year sadc_ecowas_trust au_trust
tab au_trust
tab year if sadc_ecowas_trust!=.
tab year if au_trust!=.*/

use /dataset_Latinobarometer_Afrobarometer_2000-2015-wide.dta, clear
drop if country_name==""
rename withinwt weight
tab country, nol

preserve
keep if country==508 
keep if au_trust!=. 
generate mozdummyau = 0 if au_trust  <6 // Tend to believe in effectiveness
replace mozdummyau = 1 if au_trust  >5 //Tend to believe - exclude those scoring 5, i.e. the undecided. 
replace mozdummyau = 0 if year > 2007 & au_trust==0 // do nothing, no help
replace mozdummyau = 0 if year > 2007 & au_trust==1 //help a little bit
replace mozdummyau = 1 if year > 2007 & au_trust==2 //  help somewhat
replace mozdummyau = 1 if year > 2007 & au_trust==3 //help a lot
collapse (mean) mozdummyau [pweight=weight], by(year) //weight on (united) country level
ed
save MOZcollapsed.dta, replace 

preserve
keep if au_trust!=. 
keep if country==466 
generate maldummyau = 0 if au_trust  <6 // Tend to believe in effectiveness
replace maldummyau = 1 if au_trust  >5 // Tend to believe - exclude those scoring 5, i.e. the undecided. 
replace maldummyau = 0 if year > 2007 & au_trust==0 // do nothing, no help
replace maldummyau = 0 if year > 2007 & au_trust==1 //help a little bit
replace maldummyau = 1 if year > 2007 & au_trust==2 //  help somewhat
replace maldummyau = 1 if year > 2007 & au_trust==3 //help a lot
collapse (mean)  maldummyau [pweight=weight], by(year) //weight on (united) country level
save MALcollapsed.dta, replace

preserve
keep if au_trust!=. 
keep if country==566 
generate nigdummyau = 0 if au_trust  <6 // Tend to believe in effectiveness
replace nigdummyau = 1 if au_trust  >5 // Tend to trust - exclude those scoring 5, i.e. the undecided.  
replace nigdummyau = 0 if year > 2007 & au_trust==0 // do nothing, no help
replace nigdummyau = 0 if year > 2007 & au_trust==1 //help a little bit
replace nigdummyau = 1 if year > 2007 & au_trust==2 //  help somewhat
replace nigdummyau = 1 if year > 2007 & au_trust==3 //help a lot
collapse (mean) nigdummyau [pweight=weight], by(year) //weight on (united) country level
save NIGcollapsed.dta, replace

preserve
keep if au_trust!=. 
keep if country==834
generate tandummyau = 0 if au_trust  <6 // Tend to believe in effectiveness
replace tandummyau = 1 if au_trust  >5 // Tend to trust - exclude those scoring 5, i.e. the undecided.  
replace tandummyau = 0 if year > 2007 & au_trust==0 // do nothing, no help
replace tandummyau = 0 if year > 2007 & au_trust==1 //help a little bit
replace tandummyau = 1 if year > 2007 & au_trust==2 //  help somewhat
replace tandummyau = 1 if year > 2007 & au_trust==3 //help a lot
collapse (mean) tandummyau [pweight=weight], by(year) //weight on (united) country level
ed
save TANcollapsed.dta, replace

preserve
keep if au_trust!=. 
keep if country==710
generate ZAFdummyau = 0 if au_trust  <6 // Tend to believe in effectiveness
replace ZAFdummyau = 1 if au_trust  >5 // Tend to trust - exclude those scoring 5, i.e. the undecided. 
replace ZAFdummyau = 0 if year > 2007 & au_trust==0 // do nothing, no help
replace ZAFdummyau = 0 if year > 2007 & au_trust==1 //help a little bit
replace ZAFdummyau = 1 if year > 2007 & au_trust==2 //  help somewhat
replace ZAFdummyau = 1 if year > 2007 & au_trust==3 //help a lot
collapse (mean) ZAFdummyau [pweight=weight], by(year) //weight on (united) country level
save ZAFcollapsed.dta, replace

preserve 
keep if au_trust!=. 
tab sadc_ecowas_trust
generate dummyau = 0 if au_trust <6 // Tend to believe in effectiveness
replace dummyau = 1 if au_trust >5 // Tend to trust - exclude those scoring 5, i.e. the undecided. 
replace dummyau = 0 if year > 2007 & au_trust==0 // do nothing, no help
replace dummyau = 0 if year > 2007 & au_trust==1 //help a little bit
replace dummyau = 1 if year > 2007 & au_trust==2 //  help somewhat
replace dummyau = 1 if year > 2007 & au_trust==3 //help a lot 
collapse (mean) dummyau [pweight=weight], by(year) //weight on (united) country level
save AB_mean_collapsed.dta, replace


use MOZcollapsed.dta, clear
merge m:m year using KENcollapsed.dta 
drop _merge
merge m:m year using MALcollapsed.dta 
drop _merge
merge m:m year using NIGcollapsed.dta 
drop _merge
merge m:m year using TANcollapsed.dta 
drop _merge
merge m:m year using ZAFcollapsed.dta
drop _merge
merge m:m year using AB_mean_collapsed.dta
drop _merge
save ab_mergedcollapsed.dta, replace 

tab year
gen wave = 2 if year==2002|year==2003
replace wave = 3 if year==2008|year==2009
replace wave = 4 if year==2012|year==2013
replace wave = 5 if year==2014|year==2015

replace ZAFdummyau = ZAFdummyau * 100  
replace tandummyau = tandummyau * 100 
replace nigdummyau = nigdummyau * 100  
replace maldummyau = maldummyau * 100  
replace kendummyau = kendummyau * 100  
replace mozdummyau = mozdummyau * 100  
replace dummyau = dummyau * 100 

collapse mozdummyau kendummyau maldummyau nigdummyau tandummyau ZAFdummyau dummyau, by(wave)
save au_mergedforgraphs.dta, replace


use au_mergedforgraphs.dta, clear
set scheme plotplain
tab wave
ed


graph twoway (line dummyau kendummyau maldummyau mozdummyau nigdummyau ZAFdummyau tandummyau wave), ///
xtitle("") title("Perceptions of AU") xtitle("") ylabel(0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%" 100 "100%") ///
ytitle("% help somewhat or help a lot") xlabel( 2 "2002-2003" 3 "2008-2009" 4 "2012-2013" 5 "2014-2015") ///
legend(order(1 "Average, all Afrobarometer countries" 2 "Kenya" 3 "Mali" 4 "Mozambique" 5 "Nigeria" 6 "South Africa" 7 "Tanzania" )) ///
saving(Fig2.2_au.gph, replace) 
 
 

